package com.escape.auth.application.controller;

import com.alibaba.fastjson.JSON;
import com.escape.auth.application.convert.AuthRolePermissionDTOConverter;
import com.escape.auth.application.dto.AuthRolePermissionDTO;
import com.escape.auth.common.entity.Result;
import com.escape.auth.domain.entity.AuthRolePermissionBO;
import com.escape.auth.domain.service.AuthRolePermissionDomainService;
import com.escape.auth.infra.basic.entity.AuthRolePermission;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * @Author: escape_zebra
 * @CreateTime: 2025-01-10  17:13
 * @Description: TODO
 * @Version: 1.0
 */
@RestController
@Slf4j
@RequestMapping("/rolePermission/")
public class RolePermissionController {

    @Resource
    private AuthRolePermissionDomainService authRolePermissionDomainService;

    /**
     * 新增角色权限
     */
    @RequestMapping("add")
    public Result<Boolean> add(@RequestBody AuthRolePermissionDTO authRolePermissionDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("RolePermissionController.add.DTO: {}", JSON.toJSONString(authRolePermissionDTO));
            }
            Preconditions.checkNotNull(authRolePermissionDTO.getRoleId(), "角色 id 不能为空!");
            Preconditions.checkArgument(!CollectionUtils.isEmpty(authRolePermissionDTO.getPermissionIdList()), "权限 id 不能为空!");

            AuthRolePermissionBO authRolePermissionBO = AuthRolePermissionDTOConverter.INSTANCE.convertBoToEntity(authRolePermissionDTO);

            return Result.ok(this.authRolePermissionDomainService.add(authRolePermissionBO));
        } catch (Exception e) {
            log.error("RolePermissionController.add.error: {}", e.getMessage(), e);
            return Result.fail("新增角色权限失败!");
        }


    }

}
